

from flask import  (jsonify,make_response,request,send_from_directory)

from src import create_app

# from do_db import check_token
from utils.do_db import check_token
import time;

app = create_app()

 
# 请求接口都要检测请求头的token
@app.before_request  
def before_request():  
    if request.endpoint !='static':
        token = request.headers.get('token')  
        token = token if token else request.form.get('token')
        now_time = int(time.time()) 
        u = None
        want_check = True
        if hasattr(app,'user'):
            u = app.user
            if app.user['expired_at'] > now_time:
                want_check = False
    
        if want_check:
            u = check_token(token)    
        if not token or not u:  
            return jsonify({"message": "身份令牌有误","status":1000}), 401   
        app.user=u


# 错误请求
@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found，请求的接口不存在'}), 404) 

if __name__ == "__main__":
    app.run()


